<template>
  <div>
    <el-card shadow="false" class="box-card">
      <div slot="header" class="clearfix">
        <el-button @click="add" type="primary">新增</el-button>
      </div>
      <div class="text item">
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column align="center" type="index" width="50" label="序号">
          </el-table-column>
          <el-table-column
            align="center"
            prop="roleName"
            label="角色名称"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="roleCode"
            label="角色编码"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="remark"
            label="备注"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="createTime"
            label="添加时间"
          ></el-table-column>
          <el-table-column align="center" prop="name" label="操作" width="200">
            <template slot-scope="scope">
              <el-button @click="edit(scope.row.id)" type="primary"
                >编辑</el-button
              >
              <el-button @click="del(scope.row)" type="danger">删除</el-button>
            </template></el-table-column
          >
        </el-table>
      </div>
      <!-- <div class="block">
        <el-pagination
          style="margin-left: -10px; margin-top: 6px; margin-bottom: -14px"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="10"
          layout="sizes, prev, pager, next"
          :total="totalNumber"
        >
        </el-pagination>
      </div> -->
      <pagination
        @limitfather="limitchange"
        @startfather="startchange"
        :totalNumber="totalNumber"
      ></pagination>
    </el-card>

    <addRole @tofather="getson" ref="son"></addRole>
  </div>
</template>

<script>
import accountApi from "@/api/authority/account/index";
import addRole from "@/components/authority/addRole";
import pagination from "@/components/common/pagination";
export default {
  components: { addRole, pagination },

  data() {
    return {
      start: 1,
      limit: 10,
      tableData: [],
      totalNumber: 0,
      form: {
        nickName: "",
        username: "",
      },
    };
  },

  created() {
    this.init();
  },

  mounted() {},

  methods: {
    // 来自子组件切换limit
    limitchange(e) {
      console.log(e);
      this.limit = e;
      this.init();
    },
    // 来自子组件切换start
    startchange(e) {
      console.log(e);
      this.start = e;
      this.init();
    },

    getson() {
      this.init();
    },
    // 新增
    add() {
      this.$refs.son.change();
    },
    // 编辑用户
    edit(item) {
      this.$refs.son.change(item);
    },
    // 删除用户
    del(item) {
      console.log(item);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        accountApi.removeRole(item.id).then((res) => {
          console.log(res);
          if (res.success) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.init();
          } else {
            this.$message.error(res.message);
          }
        });
      });
    },
    init(Data) {
      accountApi.findRolesByPage(this.start, this.limit, Data).then((res) => {
        console.log(res);
        this.tableData = res.data.rows;
        this.totalNumber = res.data.total;
      });
    },
    // 切换每页显示多少个
    handleSizeChange(e) {
      console.log(e);
      this.limit = e;
      this.init();
    },
    // 切换页码
    handleCurrentChange(e) {
      console.log(e);
      this.start = e;
      this.init();
    },
  },

  computed: {},

  watch: {},
};
</script>

<style scoped lang='scss'>
.box-card {
  width: 97%;
  margin: 15px auto;
}
</style>